<?php
namespace app\staff\model;
use app\common\model\Common;
use think\Db;
class RepairCount extends Common
{
	protected $table = 'mps_repair';

	/**
	* 维修金额统计
	* @param array 	$admin 		用户信息
	*/
	public function repairCount($where, $order, $search, $royalty_money)
	{
		if($where === false) return ['repair_money'=>0,'parts_money'=>0,'people_money'=>0,'royalty_money'=>0,'repair_num'=>0];
		$data['repair_money'] = $this->repairSum('repair_money', $where, $order, $search);/*维修总额*/
		$data['parts_money'] = $this->repairSum('parts_money', $where, $order, $search);/*维修总成本*/
		$data['people_money'] = $this->repairSum('people_money', $where, $order, $search);/*维修总盈利*/
		$data['royalty_money'] = $this->repairSum($royalty_money, $where, $order, $search);/*维修提成*/
		$data['repair_num'] = $this->repairNum($where, $order, $search);
		return $data;
	}
	/**
	* 管理者-维修金额统计
	*/
	public function oneRepairCount($admin)
	{	
		if($admin['admin_type'] == 2){/*营业员*/
			$royalty_money = 'sale_t_money';
			$key = 'admin_id';
		}else if($admin['admin_type'] == 3){/*维修员*/
			$royalty_money = 'repair_t_money';
			$key = 'technician_id';
		}
		$data['repair_money'] = $this->oneRepairSum('repair_money', $admin['admin_id'], $key);/*维修总额*/
		$data['parts_money'] = $this->oneRepairSum('parts_money', $admin['admin_id'], $key);/*维修总成本*/
		$data['people_money'] = $this->oneRepairSum('people_money', $admin['admin_id'], $key);/*维修总盈利*/
		$data['royalty_money'] = $this->oneRepairSum($royalty_money, $admin['admin_id'], $key);/*维修提成*/
		$data['repair_num'] = $this->oneRepairNum($admin['admin_id'], $key);
		return $data;
	}
	/****************************** 私有 ******************************/

	/**
	* 管理者-付款并取机
	* 维修金额统计
	*/
	private function oneRepairSum($field, $id, $key)
	{
		return $this->where($key, $id)->where('state',8)->sum($field);
	}
	/**
	* 管理者-付款并取机
	* 维修单总数
	*/
	private function oneRepairNum($id, $key)
	{
		return $this->where($key, $id)->where('state',8)->count();
	}
	/**
	* 获取维修单总数
	* @param intger $admin_id 管理员id
	*/
	public function repairNum($where, $order, $search)
	{
		return $this->where($where)->where($search)->order($order)->count();
	}
	/**
	* 维修所有金额统计
	*/
	private function repairSum($field, $where, $order, $search)
	{
		return $this->where($where)->where($search)->order($order)->sum($field);
	}
}